package com.itfreer.gis.map.configuration;

import com.itfreer.jwt.FilterPara;
import com.itfreer.jwt.ModeType;
import com.itfreer.jwt.filter.CoreFilter;
import com.itfreer.jwt.filter.ExcludeUrlInterception;
import com.itfreer.power.TokenPowerFilterInterception;
import com.itfreer.power.UserPowerFilterInterception;
import com.itfreer.sso.client.JwtSsoFilterInterception;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * 过滤器配置
 */
@Configuration
public class WebFilterConfiguration {

	/**
	 * 启动核心过滤器
	 * 
	 * @return
	 */
	@Bean
	public FilterRegistrationBean<CoreFilter> coreFilter() {
		FilterRegistrationBean<CoreFilter> registration = new FilterRegistrationBean<>();
		// 注入过滤器
		registration.setFilter(new CoreFilter());
		// 过滤规则
		registration.addUrlPatterns("/*");
		// 过滤器名称
		registration.setName("coreFilter");
		// 过滤器顺序
		registration.setOrder(1);
		return registration;
	}

	/**
	 * 资源过滤参数
	 * 
	 * @return
	 */
	@Bean(name = "filterPara")
	public FilterPara filterPara() {
		FilterPara filterPara = new FilterPara();
		filterPara.setMode(ModeType.Protect);
		List<String> urls = new ArrayList<>();
//		// 登录资源
//		urls.add("/static/**");
//		urls.add("/styles/**");
//		// druid资源
//		urls.add("/druid/**");
//		// api查看资源
//		urls.add("/webjars/**");
//		urls.add("/swagger-resources/**");
//		urls.add("/swagger-resources");
//		urls.add("/swagger-ui.html/**");
//		urls.add("/swagger-ui.html");
//		urls.add("/v2/api-docs");
		filterPara.setUrls(urls);
		return filterPara;
	}

	/**
	 * 资源过滤过滤器 指定的资源无需权限
	 * 
	 * @return
	 */
	@Bean(name = "excludeUrlInterception")
	public ExcludeUrlInterception excludeUrlInterception(FilterPara filterPara) {
		ExcludeUrlInterception interception = new ExcludeUrlInterception();
		interception.setIndex(1);
		interception.setFilterPara(filterPara);
		return interception;
	}

	/**
	 * 系统权限过滤器 实现系统接口权限调用
	 * 
	 * @return
	 */
	@Bean(name = "tokenPowerFilterInterception")
	public TokenPowerFilterInterception tokenPowerFilterInterception() {
		TokenPowerFilterInterception interception = new TokenPowerFilterInterception();
		interception.setIndex(2);
		return interception;
	}

	/**
	 * 单点登录过滤器
	 * 
	 * @return
	 */
	@Bean(name = "ssoFilterInterception")
	public JwtSsoFilterInterception ssoFilterInterception() {
		JwtSsoFilterInterception interception = new JwtSsoFilterInterception();
		interception.setIndex(3);
		return interception;
	}

	/**
	 * 用户权限过滤器
	 * 
	 * @return
	 */
	@Bean(name = "userPowerFilterInterception")
	public UserPowerFilterInterception userPowerFilterInterception() {
		UserPowerFilterInterception interception = new UserPowerFilterInterception();
		interception.setIndex(4);
		return interception;
	}
}